package com.zut.edu;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.View;
import android.widget.TextView;

import com.qmuiteam.qmui.util.QMUIPackageHelper;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;
import com.zut.edu.fragment.WebExplorerFragment;

import java.text.SimpleDateFormat;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AboutActivity extends AppCompatActivity {

    @BindView(R.id.version)
    TextView mVersionTextView;
    @BindView(R.id.about_list)
    QMUIGroupListView mAboutGroupListView;
    @BindView(R.id.copyright)
    TextView mCopyrightTextView;
    @BindView(R.id.about_topbar)
    QMUITopBarLayout abouttopbar;

    FragmentManager fragmentManager;
    FragmentTransaction  transaction;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        QMUIStatusBarHelper.translucent(this);
        ButterKnife.bind(this);
        initLink();
        initTopBar();
    }

    private void initTopBar() {
        abouttopbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        abouttopbar.setTitle("关于");

    }

    private void replaceFragment(Fragment fragment) {
        fragmentManager =getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment, fragment);  //fr_container不能为fragment布局，可使用线性布局相对布局等。
        transaction.commitNow();
    }


    private void initLink(){
        mVersionTextView.setText(QMUIPackageHelper.getAppVersion(this));
        QMUIGroupListView.newSection(this)
                .addItemView(mAboutGroupListView.createItemView("访问官网"), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String url = "https://www.zut.edu.cn/";
                        Bundle bundle = new Bundle();
                        bundle.putString("EXTRA_URL", url);
                        bundle.putString("EXTRA_TITLE","about");
                        WebExplorerFragment fragment = new WebExplorerFragment();
                        fragment.setArguments(bundle);
                        replaceFragment(fragment);

                    }
                })
                .addItemView(mAboutGroupListView.createItemView("Gitee"), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String url = "https://gitee.com/yejiahong/NFCsystem2021";
                        Bundle bundle = new Bundle();
                        bundle.putString("EXTRA_URL", url);
                        bundle.putString("EXTRA_TITLE", "gitee");
                        WebExplorerFragment fragment = new WebExplorerFragment();
                        fragment.setArguments(bundle);
                        replaceFragment(fragment);
                    }
                })
                .addTo(mAboutGroupListView);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy", Locale.CHINA);
        String currentYear = dateFormat.format(new java.util.Date());
        mCopyrightTextView.setText(String.format(getResources().getString(R.string.about_copyright), currentYear));
    }
}
